<?php
	require_once(dirname(__FILE__)."/session_auth.php");
	// we don't have to update $_SESSION
	session_write_close();
?>
<?php
	require_once(dirname(__FILE__)."/config.php");
	require_once(dirname(__FILE__)."/class/server/ProxyDao.php");
	require_once(dirname(__FILE__)."/response.php");
?>
<?php
/*
 * Proxy List
 * Created: 200904 (TT Tsai)
 */
	function removeProxyDaoPassword(&$newplist,$plist)
	{
		//remove password
		foreach($plist as $proxy)
		{
			$temp=&$proxy['hostinfo'];

            //settype is because
            //if $temp = "{'a','b'}";
            //use $temp['password'] will get "{"
            //will cause error
            //so use settype to change $temp tostring
            if(is_string($temp)){
                settype($temp,'string');
            }

			if(isset($temp['password']))
			{
				if ($temp['password'] != ''){
					$temp['password']= '*****';
				}
				$newplist[]=$proxy;
			}
			else{
				$newplist[]=$proxy;
			}						
		}
	}


	// load data from POST string
	$widgetRequest = json_decode($HTTP_RAW_POST_DATA,true);
	if( $widgetRequest == null ) {
		// input format error
		make_error_response( PLIST_ERRCODE_INVALIDFORMAT, PLIST_ERRCODE_INVALIDFORMAT_MSG);
		exit;
        }

	// check act
	if( !isset($widgetRequest["act"]) ) {
		// we can do nothing without 'act'
		make_error_response( PLIST_ERRCODE_NOACT, PLIST_ERRCODE_NOACT_MSG);
		exit;
	}

	// userid
	if( !isset($_SESSION['uid']) ) {
		make_error_response( PLIST_ERRCODE_WFERR, PLIST_ERRCODE_WFERR_MSG.": retrieving userid failed");
		exit;
	}
	$userid = $_SESSION['uid'];

	// create ServerList object
	$slistobj = new WFProxyDao( $wfconf_dbconfig, $userid);
	if( $slistobj->isFailed() ) {
		make_error_response( PLIST_ERRCODE_DBINIT, PLIST_ERRCODE_DBINIT_MSG.": ".$slistobj->getErrMessage());
		exit;
	}

	switch($widgetRequest['act']) {
	case "add":
		// check parameters
		if( !isset($widgetRequest["name"]) || !isset($widgetRequest["proxyinfo"]) ) {
			make_error_response( PLIST_ERRCODE_LACKINPUT, PLIST_ERRCODE_LACKINPUT_MSG);
			exit;
		}
		// add
		$slistobj->add( $widgetRequest["name"], $widgetRequest["proxyinfo"]);
		if( $slistobj->isFailed() ) {
			make_error_response( PLIST_ERRCODE_DBERR, PLIST_ERRCODE_DBERR_MSG.": ".$slistobj->getErrMessage());
			exit;
		}
		// get the id of the host
		$hostid = $slistobj->getlastid();
		make_successful_response( PLIST_OK_ADD_MSG, $hostid);
		break;
	case "delete":
		// check parameters
		if( !isset($widgetRequest["proxyid"]) ) {
			make_error_response( PLIST_ERRCODE_LACKINPUT, PLIST_ERRCODE_LACKINPUT_MSG);
			exit;
		}
		// delete
		$slistobj->del($widgetRequest["proxyid"]);
		if( $slistobj->isFailed() ) {
			make_error_response( PLIST_ERRCODE_DBERR, PLIST_ERRCODE_DBERR_MSG.": ".$slistobj->getErrMessage());
			exit;
		}
		make_successful_response( PLIST_OK_DEL_MSG, null);
		break;
	case "update":
		// check parameters
		if( !isset($widgetRequest["proxyid"]) || !isset($widgetRequest["name"]) || !isset($widgetRequest["proxyinfo"]) ) {
			make_error_response( PLIST_ERRCODE_LACKINPUT, PLIST_ERRCODE_LACKINPUT_MSG);
			exit;
		}
		$slistobj->update( $widgetRequest["proxyid"], $widgetRequest["name"], $widgetRequest["proxyinfo"]);
		if( $slistobj->isFailed() ) {
			make_error_response( PLIST_ERRCODE_DBERR, PLIST_ERRCODE_DBERR_MSG.": ".$slistobj->getErrMessage());
			exit;
		}
		make_successful_response( PLIST_OK_UPDATE_MSG, null);
		break;
	case "getall":
		$slist = $slistobj->getall();
		if( $slistobj->isFailed() ) {
			make_error_response( PLIST_ERRCODE_DBERR, PLIST_ERRCODE_DBERR_MSG.": ".$slistobj->getErrMessage());
			exit;
		}
		removeProxyDaoPassword($newslist,$slist);
		make_successful_response( null, $newslist);
		break;
	case "get":
		// check parameters
		if( !isset($widgetRequest["proxyid"]) ) {
			make_error_response( PLIST_ERRCODE_LACKINPUT, PLIST_ERRCODE_LACKINPUT_MSG);
			exit;
		}
		// get
		$slist = $slistobj->getinfo($widgetRequest["proxyid"]);
		if( $slistobj->isFailed() ) {
			make_error_response( PLIST_ERRCODE_DBERR, PLIST_ERRCODE_DBERR_MSG.": ".$slistobj->getErrMessage());
			exit;
		}
		make_successful_response( null, $slist);
		break;
	default:
		make_error_response( PLIST_ERRCODE_CMDERR, PLIST_ERRCODE_CMDERR_MSG);
		break;
	}
?>
